package com.atguigu.spring.tx.dao.impl;

import com.atguigu.spring.tx.bean.Account;
import com.atguigu.spring.tx.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class AccountDaoImpl implements AccountDao {
    @Autowired
    JdbcTemplate template;

    public Account getAccountById(int id) {
        String sql = "select * from account where id = ?";
        Account account = template.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class), id);
        return account;
    }

    @Override
    public int customBalance(int id, BigDecimal amount) {
        String sql = "update account set balance = balance - ? where id = ?";
        int updated = template.update(sql, amount, id);
        return updated;
    }
}
